package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiProjectExtEditBo;
import com.piggy.logi.sys.bo.LogiProjectQueryBo;
import com.piggy.logi.sys.service.ILogiProjectService;
import com.piggy.logi.sys.vo.LogiProjectExtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 项目管理Controller
 *
 * @author piggy
 * @date 2023-08-18
 */
@Api(value = "项目管理控制器", tags = {"项目管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/project")
public class LogiProjectController extends BaseController {

    private final ILogiProjectService iLogiProjectService;

    /**
     * 查询项目管理列表
     */
    @ApiOperation("查询项目管理列表")
    //@SaCheckPermission("sys:project:list")
    @PostMapping("/list")
    public TableDataInfo<LogiProjectExtVo> list(@Validated @RequestBody LogiProjectQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return iLogiProjectService.queryPageList(bo);
    }

    /**
     * 导出项目管理列表
     */
    @ApiOperation("导出项目管理列表")
    //@SaCheckPermission("sys:project:export")
    @LogiLog(title = "项目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiProjectQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiProjectExtVo> list = iLogiProjectService.queryList(bo);
        ExcelUtil<LogiProjectExtVo> util = new ExcelUtil<LogiProjectExtVo>(LogiProjectExtVo.class);
        util.exportExcel(response, list, "项目管理");
    }

    /**
     * 获取项目管理详细信息
     */
    @ApiOperation("获取项目管理详细信息")
    //@SaCheckPermission("sys:project:query")
    @GetMapping("/{id}")
    public R<LogiProjectExtVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iLogiProjectService.queryById(id));
    }

    /**
     * 新增项目管理
     */
    @ApiOperation("新增项目管理")
    //@SaCheckPermission("sys:project:add")
    @LogiLog(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiProjectExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiProjectService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改项目管理
     */
    @ApiOperation("修改项目管理")
    //@SaCheckPermission("sys:project:edit")
    @LogiLog(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiProjectExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiProjectService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除项目管理
     */
    @ApiOperation("删除项目管理")
    //@SaCheckPermission("sys:project:remove")
    @LogiLog(title = "项目管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return R.ok(iLogiProjectService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
